package com.epam.klymanov.tools;

import java.awt.image.BufferedImage;
import java.io.Serializable;

public class PollutionMatrix implements Serializable {

	private static final long serialVersionUID = 1L;

	private double leftUpLatitude;
	private double leftUpLongitude;
	private double rightDownLatitude;
	private double rightDownLongitude;
	private double step;
	private int height;
	private int width;
	private int[][] matrix;

	public PollutionMatrix(double leftUpLatitude, double leftUpLongitude,
			double rightDownLatitude, double rightDownLongitude, double step) {
		super();
		this.leftUpLatitude = leftUpLatitude;
		this.leftUpLongitude = leftUpLongitude;
		this.rightDownLatitude = rightDownLatitude;
		this.rightDownLongitude = rightDownLongitude;
		this.step = step;
		height = (int) ((leftUpLatitude - rightDownLatitude) / step);
		width = (int) ((rightDownLongitude - leftUpLongitude) / step);
		matrix = new int[width][height];
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public int[][] getMatrix() {
		return matrix;
	}

	public int getX(double longitude) {
		if (longitude > rightDownLongitude) {
			return 0;
		}
		if (longitude < leftUpLongitude) {
			return 0;
		}
		int x = (int) (-(leftUpLongitude - longitude) / step);

		return x;

	}

	public int getY(double latitude) {
		if (latitude < rightDownLatitude) {
			return 0;
		}
		if (latitude > leftUpLatitude) {
			return 0;
		}
		int y = (int) ((leftUpLatitude - latitude) / step);

		return y;

	}

	public double getLatitude(int y) {
		if (y > height) {
			return 0;
		}
		if (y < 0) {
			return 0;
		}
		double latitude = leftUpLatitude - step * ((double) y);
		return latitude;
	}

	public double getLongitude(int x) {
		if (x > width) {
			return 0;
		}
		if (x < 0) {
			return 0;
		}
		double longitude = leftUpLongitude + step * ((double) x);
		return longitude;
	}

	public boolean addSpot(double latitude, double longitude, int pollution) {
		if (latitude < rightDownLatitude) {
			return false;
		}
		if (latitude > leftUpLatitude) {
			return false;
		}
		if (longitude > rightDownLongitude) {
			return false;
		}
		if (longitude < leftUpLongitude) {
			return false;
		}

		int x = getX(latitude);
		int y = getY(longitude);

		matrix[x][y] = pollution;

		return false;
	}

	public boolean addSpot(int x, int y, int pollution) {
		if (x < 0) {
			return false;
		}
		if (x > width) {
			return false;
		}
		if (y > height) {
			return false;
		}
		if (y < 0) {
			return false;
		}

		matrix[x][y] = pollution;

		return false;
	}

	public boolean readFromImage(BufferedImage image) {
		BufferedImage resizedImage = ImageProcessor.resizeImage(image, width,
				height);
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				int rgb = resizedImage.getRGB(i, j);
				matrix[i][j] = 255 - (((rgb >> 16) & 0xff) + ((rgb >> 8) & 0xff)) / 2;
			}
		}
		return false;
	}

	public void showMatrix() {
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				System.out.printf("%4d", matrix[i][j]);
			}
			System.out.println();
		}
	}

	public double getStep() {
		return step;
	}

}
